package com.phf.utils;

import lombok.Getter;

public class SizeCalculator {

    /**
     * 根据数据数量计算调整后的尺寸
     * @param count         数据个数
     * @param initialWidth  初始宽度
     * @param initialHeight 初始高度
     * @return 调整后的尺寸对象
     */
    public static Dimension calculate(Integer count, Double initialWidth, Double initialHeight) {
        if (count <= 0) throw new IllegalArgumentException("数量必须大于0");

        // 计算网格行列数
        Grid grid = calculateGrid(count);

        // 根据网格分割计算实际尺寸
        double width = initialWidth / grid.columns;
        double height = initialHeight / grid.rows;

        return new Dimension(width, height);
    }

    // 计算最佳网格分布
    private static Grid calculateGrid(int count) {
        int columns = 1;
        int rows = 1;

        while (columns * rows < count) {
            // 动态扩展策略：列优先扩展，保持列>=行
            if (columns == rows) {
                columns *= 2; // 列数翻倍
            } else {
                rows *= 2;   // 行数翻倍
            }
        }
        return new Grid(columns, rows);
    }

    // 网格结构体
    private static class Grid {
        final int columns;
        final int rows;

        Grid(int columns, int rows) {
            this.columns = columns;
            this.rows = rows;
        }
    }

    // 尺寸结构体
    @Getter
    public static class Dimension {
        private final double width;
        private final double height;

        public Dimension(double width, double height) {
            this.width = width;
            this.height = height;
        }

//        @Override
//        public String toString() {
//            return String.format("[%.1f x %.1f]", width, height);
//        }
    }
}
